﻿using System;
using System.Linq;
using System.Windows.Forms;
using Relocation.Com;
using Relocation.Data;
using Relocation.UI.Interface;
using Relocation.Base;

namespace Relocation.UI
{
	public partial class ProjectList : BaseProjectList, IPrintForm
	{
		public ProjectList()
			: base()
		{
			InitializeComponent();
			Initialize();

		}

		public ProjectList(Session session) :
			base(session)
		{
			InitializeComponent();
			Initialize();
		}

		private void Initialize()
		{
			this.S_Field_EnableData.Tag = new ControlTag(null, null, new SearchInfo(Project.GetPropName(t=>t.created), SearchInfo.SearchFieldTypes.DateTime));
			this.S_Field_Name.Tag = new ControlTag(null, null, new SearchInfo(Project.GetPropName(t => t.name)));
			this.S_Field_Org.Tag = new ControlTag(null, null, new SearchInfo(Project.GetPropName(t => t.organization)));
			this.SortField = Project.GetPropName(t => t.updated);
			this.ObjectQuery = this.Session.DataModel.Projects;
		}

		private void ButtonAdd_Click(object sender, EventArgs e)
		{
			try
			{
				using (ProjectsWindow projectWindow = new ProjectsWindow(this.Session))
				{
					if (DialogResult.OK.Equals(projectWindow.ShowDialog()))
						this.Reload();
				}
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				this.MessageBoxShowInfo("操作失败！");
			}
		}

		private void ButtonEdit_Click(object sender, EventArgs e)
		{
			try
			{
				Project project = this.GetSelectEntity();
				if (project != null)
				{
					using (ProjectsWindow projectWindow = new ProjectsWindow(this.Session, project))
					{
						if (DialogResult.OK.Equals(projectWindow.ShowDialog()))
						{
							this.Reload();
						}
					}
				}
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				this.MessageBoxShowInfo("操作失败！");
			}
		}

		private void ButtonDel_Click(object sender, EventArgs e)
		{
			try
			{
				Project entity = this.GetSelectEntity();
				if (entity == null)
					return;
				if (entity.project_id.Equals(this.Session.Project.project_id))
				{
					MessageBoxShowInfo("该项目是当前项目，不允许删除！");
					return;
				}
				if (entity.IsUsed())
				{
					MessageBoxShowInfo("该项目下已包含拆迁户数据，不允许删除！");
					return;
				}
				this.DeleteEntity(entity);
			} catch (Exception ex)
			{
				this.Log.Error(ex.GetInnerExceptionMessage());
				MessageBoxError("记录删除失败！");
			}
		}		


		#region IPrintForm 成员

		public void Print()
		{
			this.print(this.dataGridView1);
		}

		#endregion

		private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			try
			{
				string _name = this.dataGridView1.Columns[e.ColumnIndex].Name;
				if (!string.IsNullOrEmpty(_name) && _name.Equals(this.Files.Name))
				{
					Project project = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as Project;
					if (project != null)
					{
						project.LoadFiles();
						if (project.Files.Any(t => t.file_type == 1))
							e.Value = "红线规划图";
						else
							e.Value = "没有文件";
						e.FormattingApplied = true;
					}
				}
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);			
			}
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				string _name = this.dataGridView1.Columns[e.ColumnIndex].Name;
				if (_name.Equals(this.Files.Name))
				{
					Project project = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as Project;
					if (project == null)
						return;
					using (ProjectPic ProjectPic = new ProjectPic(this.Session, project))
					{
						ProjectPic.ShowDialog();
					}					
				}
			} catch (Exception ex)
			{				
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);
				this.MessageBoxShowInfo("操作失败！");
			}
		}
	}
}
